package com.example.demo01.Socket.Client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;




@Slf4j
@Component
public class  ConnectChannelInboundHandler extends SimpleChannelInboundHandler {




    private static ConnectChannelInboundHandler connectChannelInboundHandler;

    @Resource
    private NettyClient client;


    @PostConstruct
    public void init(){

        connectChannelInboundHandler=this;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        log.info("channelInactive....");
        final    EventLoop eventLoop = ctx.channel().eventLoop();
        eventLoop.schedule(new Runnable() {
            @Override
            public void run() {

                log.info("run ........"+connectChannelInboundHandler.client.getClass().getName());
                connectChannelInboundHandler.client.createBootstrap(new Bootstrap(), eventLoop);
            }
        }, 2l, TimeUnit.SECONDS);
        super.channelInactive(ctx);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {

    }
}
